package org.yi.fc.controller.admin.content;

import java.util.Date;

import org.apache.commons.lang.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yi.fc.controller.base.BaseController;
import org.yi.fc.entity.FCategory;
import org.yi.fc.entity.FPost;
import org.yi.fc.enums.PostStatusEnum;
import org.yi.fc.enums.PostTypeEnum;
import org.yi.fc.model.Pager;

import com.jfinal.plugin.activerecord.Page;

public class PostsController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(PostsController.class);
	
	public void list(){
		Pager pager = getPager();
		FPost fcontent = new FPost();
		String keyword = this.getPara("keyword");
		String ftype = this.getPara("ftype");
		String fstatus = this.getPara("fstatus");
		Page<FPost> page = fcontent.query(pager.getPn(), pager.getSize(), keyword, ftype, fstatus);
		setAttr("page", page);
		setAttr("fcontent", fcontent);
		setAttr("keyword", keyword);
		
		setAttr("ftype", PostTypeEnum.values());
		setAttr("pstatus", PostStatusEnum.values());
		setAttr("s_ftype", ftype);
		setAttr("s_status", fstatus);
		
		render("list.html");
	}
	
	/**
	 * 编辑文章
	 */
	public void edit(){
		try{
			Long id = getParaToLong("articleId");
			FPost fcontent = FPost.dao.findById(id);
			fcontent.set("content",StringEscapeUtils.unescapeHtml(fcontent.get("content").toString()));
			setAttr("post", fcontent);
		}catch(Exception e){
			addMessage("该内容不存在");
		}
//		redirect("/admin/post/add");
		forwardAction("/admin/post/add");
	}
	
	public void index(){
		list();
	}
	
	public void add(){
		setAttr("cats", FCategory.dao.getAll());
		setAttr("ftype", PostTypeEnum.values());
		render("add.html");
	}
	
	/**
	 * 保存文章
	 */
	public void save(){
		try{
			FPost f = getModel(FPost.class);
			String content = f.getStr("content");
			content = StringEscapeUtils.escapeHtml(content);
			
			f.set("content", content);
			f.set("create_time", new Date());
			
			f.save(f);
			setAttr("fcontent", f);
			
			addMessage("提交文章成功！");
			setAttr("msgStatus",SUCCESS)
			.setAttr("fcontent", f);
			
			list();
		}catch(Exception e){
			addError("提交文章失败" + e.getMessage());
			setAttr("msgStatus",ERROR);
			logger.error("提交文章失败",e);
			add();
		}
		
	}

	/**
	 * 删除文章（不是真的将文章删除，而是改变文章的状态）
	 */
	public void trash(){
		try{
			Long id = getParaToLong("articleId");
			FPost.dao.findById(id)
			.set("status", PostStatusEnum.DELETE.getDesc())
			.update();
			addMessage("删除文章成功");
		}catch(Exception e){
			addMessage("删除文章失败:\n" + e.getMessage());
		}finally{
			list();
		}
	}
	
	/**
	 * 删除指定的文章
	 */
	public void delContents(){
		String [] ids = getParaValues("contentId");
		String msg = "";
		try{
			for(String id : ids){
				FPost.dao.findById(id)
				.set("status", PostStatusEnum.DELETE.getName())
				.update();
			}
			msg = "批量删除成功！";
		}catch(Exception e){
			msg = "批量删除失败：\n" + e.getMessage();
		}finally{
			addMessage(msg);
			list();
		}
		
	}
	
	/**
	 * 禁用文章成功
	 */
	public void forbidContent(){
		try{
			Long id = getParaToLong("articleId");
			FPost.dao.findById(id)
			.set("status", PostStatusEnum.FORBID.getName())
			.update();
			addMessage("禁用文章成功");
		}catch(Exception e){
			addMessage("禁用文章失败:\n" + e.getMessage());
		}finally{
			list();
		}
	}
	
	/**
	 * 禁用指定的文章
	 */
	public void forbidContents(){
		String [] ids = getParaValues("contentId");
		String msg = "";
		try{
			for(String id : ids){
				FPost.dao.findById(id)
				.set("content_status_en", PostStatusEnum.FORBID.getName())
				.update();
			}
			msg = "批量禁用成功！";
		}catch(Exception e){
			msg = "批量禁用失败：\n" + e.getMessage();
		}finally{
			addMessage(msg);
			list();
		}
	}
	
	/**
	 * 启用指定的文章
	 */
	public void repubContents(){
		String [] ids = getParaValues("contentId");
		String msg = "";
		try{
			for(String id : ids){
				FPost.dao.findById(id)
				.set("content_status_en", PostStatusEnum.PUBLISH.getName())
				.set("content_status_cn", PostStatusEnum.PUBLISH.getDesc())
				.update();
			}
			msg = "批量启用成功！";
		}catch(Exception e){
			msg = "批量启用失败：\n" + e.getMessage();
		}finally{
			addMessage(msg);
			list();
		}
	}
	
	public void reViewContent(){
		String id = getPara("cid");
		FPost fContent = FPost.dao.findById(id);
		logger.trace("解码前的数据:" + fContent.getStr("content"));
		fContent.set("content", StringEscapeUtils.unescapeHtml(StringEscapeUtils.unescapeHtml(fContent.getStr("content"))));
		logger.trace("解码后的数据:" + fContent.getStr("content"));
		setAttr("fContent", fContent);
		render("/themes/html/testContent.html");
	}
	
}
